package ru.pb.game.network.threading;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import ru.pb.game.configs.GameThreadingConfig;
import ru.pb.game.network.engine.auth.packets.AuthServerToGameServerPacket;
import ru.pb.global.network.engine.NioClient;
import ru.pb.global.threading.PriorityThreadFactory;

public class AsynchronousPacketRunner implements PacketRunner
{
	private ExecutorService _executor;

	public AsynchronousPacketRunner()
	{
		_executor = new ThreadPoolExecutor(GameThreadingConfig.PACKET_THREAD_POOL_SIZE, GameThreadingConfig.PACKET_THREAD_POOL_SIZE + 2, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("Normal Packet Pool", DEFAULT_THREAD_PRIORITY));
	}

	@Override
	public void runPacket(AuthServerToGameServerPacket packet)
	{
		if( !_executor.isShutdown())
		{
			_executor.execute(packet);
		}
	}

	@Override
	public void removeContext(NioClient client)
	{}

	@Override
	public void shutdown()
	{
		_executor.shutdown();
	}
}